package com.novax.ex.activity.provider.controller;

import cn.hutool.core.bean.BeanUtil;
import com.novax.ex.activity.infrastructure.entity.MallProductBanner;
import com.novax.ex.activity.open.api.inner.MallProductBannerInnerApi;
import com.novax.ex.activity.open.model.query.MallProductBannerQuery;
import com.novax.ex.activity.open.model.request.MallProductBannerRequest;
import com.novax.ex.activity.open.model.response.MallProductBannerResponse;
import com.novax.ex.activity.provider.service.MallProductBannerService;
import com.novax.ex.common.constant.InternationalCode;
import com.novax.ex.common.constant.StatusCode;
import com.novax.ex.common.results.ReturnResult;
import com.novax.ex.common.util.PageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Description:轮播图片配置
 *
 * @author Deucalion@novax.email
 * @date 2023/2/21 17:21
 */
@Slf4j
@RestController
public class MallProductBannerController extends BaseActivityController implements MallProductBannerInnerApi {
    @Resource
    private MallProductBannerService mallProductBannerService;

    @Override
    public ReturnResult add(String language, Long adminId, MallProductBannerRequest request, BindingResult bindingResult) {
        if(bindingResult.hasErrors()){
            return ReturnResult.fail(getInternational(language, InternationalCode.LACK_VALUE));
        }
        long num = mallProductBannerService.countBannerNum(request.getMallProductId());
        if(num >= 5){
            String content = MessageFormat.format(getInternational(language, InternationalCode.UPLOAD_MAX), 5);
            return new ReturnResult<>(StatusCode.ERROR,content);
        }
        MallProductBanner entity = BeanUtil.copyProperties(request, MallProductBanner.class);
        entity.setId(null);
        entity.setCreator(adminId);
        boolean result = mallProductBannerService.addSelective(entity);
        if (result) {
            return ReturnResult.success(getInternational(language, InternationalCode.OPERATION_SUCCESS));
        }
        return ReturnResult.fail(getInternational(language, InternationalCode.OPERATION_ERROR));
    }

    @Override
    public ReturnResult modify(String language, Long adminId, MallProductBannerRequest request,BindingResult bindingResult) {
        if(null == request.getId()){
            return ReturnResult.fail(getInternational(language, InternationalCode.LACK_VALUE));
        }
        if(bindingResult.hasErrors()){
            return ReturnResult.fail(getInternational(language, InternationalCode.LACK_VALUE));
        }
        MallProductBanner old = mallProductBannerService.find(request.getId());
        if(null == old){
            return ReturnResult.fail(getInternational(language, InternationalCode.NOT_EXIST));
        }
        MallProductBanner entity = BeanUtil.copyProperties(request, MallProductBanner.class);
        entity.setUpdater(adminId);
        boolean result = mallProductBannerService.modifySelective(entity);
        if (result) {
            return ReturnResult.success(getInternational(language, InternationalCode.OPERATION_SUCCESS));
        }
        return ReturnResult.fail(getInternational(language, InternationalCode.OPERATION_ERROR));
    }

    @Override
    public ReturnResult remove(Long id, String language) {
        MallProductBanner old = mallProductBannerService.find(id);
        if(null == old){
            return ReturnResult.fail(getInternational(language, InternationalCode.NOT_EXIST));
        }
        boolean result = mallProductBannerService.remove(id);
        if (result) {
            return ReturnResult.success(getInternational(language, InternationalCode.OPERATION_SUCCESS));
        }
        return ReturnResult.fail(getInternational(language, InternationalCode.OPERATION_ERROR));
    }

    @Override
    public ReturnResult<PageUtil<MallProductBannerResponse>> list(String language, MallProductBannerQuery query) {
        PageUtil<MallProductBannerResponse> pages = new PageUtil<>(query);
        Map<String, Object> condition = new HashMap<>();
        condition.put("query", query);
        pages.setCondition(condition);
        mallProductBannerService.findPage(pages,MallProductBannerResponse.class);
        return ReturnResult.success(getInternational(language, InternationalCode.QUERY_SUCCESS), pages);
    }

    @Override
    public ReturnResult<MallProductBannerResponse> detail(Long id, String language) {
        MallProductBanner old = mallProductBannerService.find(id);
        if(null == old){
            return ReturnResult.fail(getInternational(language, InternationalCode.NOT_EXIST));
        }
        MallProductBannerResponse response = BeanUtil.copyProperties(old, MallProductBannerResponse.class);
        return ReturnResult.success(getInternational(language, InternationalCode.QUERY_SUCCESS), response);
    }
}
